package com.xiaoke.domain;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

/**
 * @Author xiaoke
 * @Description 统一返回响应类
 * @Date 2023/7/23 19:22
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class R<T> implements Serializable {
    private static final String SUCCESS = "操作成功";
    private static final String ERROR = "操作失败";

    private Integer code;
    private String msg;
    private T data;


    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static<T> R<T> ok(T data) {
        return new R<T>(200,SUCCESS,data);
    }
    public static<T> R<T> ok(String msg) {
        return new R<T>(200,msg,null);
    }
    public static<T> R<T> ok() {
        return new R<T>(200,SUCCESS,null);
    }

    public static<T> R<T> error(T data) {
        return new R<T>(400,ERROR,data);
    }
    public static<T> R<T> error(String msg) {
        return new R<T>(400,msg,null);
    }
    public static<T> R<T> error() {
        return new R<T>(400,ERROR,null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
